package com.heima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.SensitiveMapper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.service.SensitiveService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author chenshan
 */
@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {

    /**
     * 分页条件查询敏感词
     * @param sensitiveDto 分页查询参数
     * @return ResponseResult
     */
    @Override
    public ResponseResult pageFindSensitives(SensitiveDto sensitiveDto) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(sensitiveDto.getPage())) {
            sensitiveDto.setPage(0);
        }

        if (ObjectUtil.isEmpty(sensitiveDto.getSize())) {
            sensitiveDto.setSize(10);
        }

        // 开启分页
        Page<WmSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());
        page(page, Wrappers.lambdaQuery(WmSensitive.class).eq(ObjectUtil.isNotEmpty(sensitiveDto.getName()), WmSensitive::getSensitives, sensitiveDto.getName()));

        PageResponseResult result = new PageResponseResult();
        result.setCurrentPage((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setTotal((int) page.getTotal());
        result.setData(page.getRecords());
        result.setHost("");

        return result;
    }

    /**
     * 添加敏感词
     * @param sensitive 敏感词信息
     * @return ResponseResult
     */
    @Override
    public ResponseResult saveSensitives(WmSensitive sensitive) {
        // 校验参数的合法性
        if (!ObjectUtil.isAllNotEmpty(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        sensitive.setCreatedTime(new Date());

        save(sensitive);

        return ResponseResult.okResult(null);
    }

    /**
     * 修改敏感词
     * @param sensitive 敏感词信息
     * @return ResponseResult
     */
    @Override
    public ResponseResult updateSensitives(WmSensitive sensitive) {
        // 校验参数的合法性
        if (ObjectUtil.isNull(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(sensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id 敏感词id
     * @return ResponseResult
     */
    @Override
    public ResponseResult delSensitivesById(String id) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
